package com.leyou.trade.web;

import com.leyou.trade.dto.PayResultDTO;
import com.leyou.trade.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取支付链接
     * @param orderId 订单的id
     * @return 链接
     */
    @GetMapping("/url/{id}")
    public ResponseEntity<String> getPayUrl(@PathVariable("id") Long orderId){
        return ResponseEntity.ok(orderService.getPayUrl(orderId));
    }

    /**
     * 处理微信支付回调
     * @return 回调结果
     */
    @PostMapping(value = "/wx/notify",produces = MediaType.APPLICATION_XML_VALUE)//强制返回结果为xml格式
    public ResponseEntity<PayResultDTO> handleWxNotify(@RequestBody Map<String,String> data){
        //System.out.println("data = " + data);
        orderService.handleWxNotify(data);
        return ResponseEntity.ok(new PayResultDTO());
    }

}
